package de.g18.it0a.jlaser.util;

import de.g18.it0a.jlaser.domain.Line;
import de.g18.it0a.jlaser.domain.Position;
import de.g18.it0a.jlaser.domain.Tile;


public class LineUtil {

    private LineUtil() {
        // prevent instantiation
    }

    public static boolean intersects(Line aLine, Tile aTile) {
        Position tileLeftCornerBottom = new Position(aTile.getPosition().getX() + 1,
                                                     aTile.getPosition().getY() + 1);
        Position tileLeftCornerTop = new Position(aTile.getPosition().getX() + 1,
                                                  aTile.getPosition().getY() + aTile.getHeight() - 1);
        Position tileRightCornerBottom = new Position(aTile.getPosition().getX() + aTile.getWidth() - 1,
                                                      aTile.getPosition().getY() + 1);
        Position tileRightCornerTop = new Position(aTile.getPosition().getX() + aTile.getWidth() - 1,
                                                   aTile.getPosition().getY() + aTile.getHeight() - 1);
        return intersectsAny(aLine, new Line(tileLeftCornerTop, tileRightCornerBottom),
                                    new Line(tileRightCornerTop, tileLeftCornerBottom));
    }

    public static boolean intersectsAny(Line aLineToCheck, Line... aOthers) {
        for (Line other : aOthers) {
            if (aLineToCheck.getLine2D().intersectsLine(other.getLine2D())) {
                return true;
            }
        }
        return false;
    }
}
